//
//  SISSMyRecommendTenantViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/1/2.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSMyRecommendTenantViewController.h"
#import "SISSCommonRentCell.h"
#import "SISSNetworkingManager+Mine.h"

static NSString *myRecommendTenantCellReuseID = @"myRecommendTenantCellReuseID";
@interface SISSMyRecommendTenantViewController ()

@property (weak, nonatomic) IBOutlet SISSBaseTableView *tableView;

/** 租客数组 */
@property (strong, nonatomic) NSArray<SISSTenantModel *> *tenantResourceArr;

@end

@implementation SISSMyRecommendTenantViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self requestMyRecommendTenantResourceData];
    
    WeakSelf
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf upDownLoadData];
    }];
}



#pragma mark - 下拉刷新加载方法
- (void)upDownLoadData {
    
    
    [self requestMyRecommendTenantResourceData];
}


#pragma mark - 加载数据
- (void)requestMyRecommendTenantResourceData {
    
    [kNetworkManager requestMyRecommendTenantWithsuccess:^(NSArray<SISSTenantModel *> *tenantResources) {
    
            self.tenantResourceArr = tenantResources;
    
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
    
                [self.tableView reloadData];
    
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshing];
            }];
    
    
        } failed:^(NSError *error) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            NSLog(@"%@",error);
        }];
    
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [tableView tableViewDisplayWithMessage:@"暂无数据" ifNecessaryForRowCount:self.tenantResourceArr.count];
    return self.tenantResourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSCommonRentCell *cell = [tableView dequeueReusableCellWithIdentifier:myRecommendTenantCellReuseID];
    
    cell.tenantModel = self.tenantResourceArr[indexPath.row];
    
    NSString *tagStr;
    UIColor *tagColor;
    switch (self.tenantResourceArr[indexPath.row].state) {
        case 0:
            tagStr = @"等待确认";
            tagColor = COLOR(0xAEC1D9);
            break;
            
        case 1:
            tagStr = @"已确认";
            tagColor = COLOR(0xff334f);
            break;
            
        case 2:
            tagStr = @"已拒绝";
            tagColor = COLOR(0xFF1F1D);
            break;
            
        default:
            break;
    }
    cell.isHideButton = self.tenantResourceArr[indexPath.row].state == 1;
    cell.recommendButton.layer.borderWidth = 0;
    [cell.recommendButton setTitleColor:tagColor forState:UIControlStateNormal];
    [cell.recommendButton setTitle:tagStr forState:UIControlStateNormal];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //    SISSEnrollPassengerDetailViewController *vc = [[SISSEnrollPassengerDetailViewController alloc] init];
    //    vc.title = @"客源详情";
    //    vc.abdId = self.tenantResourceArr[indexPath.row].abdId;
    //    self.tenantResourceArr[indexPath.row].read = 1;
    //    [self.tableView reloadData];
    //    [self.navigationController pushViewController:vc animated:YES];
}




#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    // tableView相关
    // 自动行高
    self.tableView.estimatedRowHeight = 80;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
    }
    // 去掉cell系统的线条
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 注册cell
    [self.tableView registerClass:[SISSCommonRentCell class] forCellReuseIdentifier:myRecommendTenantCellReuseID];
}

#pragma mark - Getter & Setter

- (NSArray<SISSTenantModel *> *)tenantResourceArr {
    if (!_tenantResourceArr) {
        _tenantResourceArr = [NSMutableArray array];
    }
    return _tenantResourceArr;
}

@end
